// subPages/integral/integralShop/integralShop.js
const app = getApp();
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'goods'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsNavList:[{class_name: '全部商品'}],
    currentGoodsNav: 0,
    classId: '',
    integral: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    loadMoreView = this.selectComponent("#loadMoreView")
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
    wx.getSystemInfo({
      success: (res) => {
        //创建节点选择器
        that.setData({
          windowWidth: res.windowWidth
        })
      },
    })
    //社区id
    if(app.globalData.communityId){
      this.setData({
        communityId: app.globalData.communityId
      })
      //获取商品分类
      util.getAjax(app.globalData.apiUrl +'/get_jf_shop_class',that.getGoodsClass,{community_id: app.globalData.communityId})
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          this.setData({
            communityId: communityId
          })
          //获取商品分类
          util.getAjax(app.globalData.apiUrl +'/get_jf_shop_class',that.getGoodsClass,{community_id: communityId})
        }
      }
    }
    //获取商品数据
    that.getShopAllData(this.data.classId)
  },

  //获取积分
  getIntegral:function(userId){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_user_info',function(res){
      console.log(res)
      if(res.data){
        that.setData({
          integral: res.data.jf
        })
      }
    },{user_id: userId})
  },

  //获取商品分类
  getGoodsClass(res){
    // console.log(res)
    var that = this
    if(res.data.length != 0){
      var navList = []
      for(var i in res.data){
        navList.push(res.data[i])
      }
      let list = that.data.goodsNavList.concat(navList)
      that.setData({
        goodsNavList: list
      })
    }
  },

  //获取商品数据
  getShopAllData:function(classId){
    //获取商品数据
    util.getAjax(app.globalData.apiUrl +'/user_get_community_jf_shop_product',this.getShopData,{p: p,class_id:classId,community_id: this.data.communityId,num: num,type: 5},this.shopDataErr)
  },

  getShopData(res){
    var that = this
    console.log(res)
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.goodsAllList.concat(res.data.list)
      }
      that.setData({
        goodsAllList: list
      })
    }else{
      that.setData({
        goodsAllList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  shopDataErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取商品失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getShopAllData(this.data.classId)
  },

  clickLoadMore: function(e) {
    this.getShopAllData(this.data.classId)
  },

  swiperChange:function(e){
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;
    if(that.data.goodsNavList.length > 3){
      //每个tab选项宽度占1/4
      var singleNavWidth = that.data.windowWidth / 4;
      //tab选项居中
      that.setData({
        navScrollLeft: (cur - 1.5) * singleNavWidth
      })
    }else{
      //每个tab选项宽度占1/3
      var singleNavWidth = that.data.windowWidth / 3;
      //tab选项居中
      that.setData({
        navScrollLeft: (cur - 1) * singleNavWidth
      })
    }
    if (that.data.currentGoodsNav == cur) {
      return false;
    } else {
      that.setData({
        currentGoodsNav: cur
      })
      // console.log(that.data.goodsNavList[that.data.currentGoodsNav])
      that.setData({
        classId: that.data.currentGoodsNav != 0?that.data.goodsNavList[that.data.currentGoodsNav].id:'',
      })
      p = 1
      that.getShopAllData(that.data.classId)
    }
  },

  //点击食品
  clickGoods:function(e){
    if(app.globalData.userId){
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../integralShopDetails/integralShopDetails?id=' + id,
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //全部订单
  ordersBtn:function(){
    wx.navigateTo({
      url: '../orders/orders'
    })
  },
  
  integralBtn:function(){
    wx.navigateTo({
      url: '/subPages/mine/integral/integral'
    })
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getIntegral(that.data.userId)
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getIntegral(that.data.userId)
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
      that.getIntegral(that.data.userId)
      //获取商品分类
      util.getAjax(app.globalData.apiUrl +'/get_jf_shop_class',that.getGoodsClass,{community_id: that.data.communityId})
      //获取商品数据
      that.getShopAllData(that.data.classId)
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getShopAllData(this.data.classId)
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
